package cn.edu.ctbu.websql.service;

import cn.edu.ctbu.websql.domain.StSelect;
import cn.edu.ctbu.websql.repository.SelectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SelectService {
    @Autowired
    private SelectRepository selectRepository;
    public Page<StSelect> getAll(Pageable pageable){
        return selectRepository.findAll(pageable);
    }
    public Optional<StSelect> getData(Integer id){
        return selectRepository.findById(id);
    }
    public StSelect update(Integer id, StSelect newStSelect){
        Optional<StSelect> selectOptional = selectRepository.findById(id);
        if(selectOptional.isPresent()){

            StSelect stSelect = selectOptional.get();

            stSelect.setGs(newStSelect.getGs());
            stSelect.setXd(newStSelect.getXd());
            stSelect.setJz(newStSelect.getJz());
            stSelect.setAcademy(newStSelect.getAcademy());

            return selectRepository.save(stSelect);
        }
        return null;
    }

    public List<StSelect> searchData(String no){
        return selectRepository.findAllByNoLike(no+"%");
    }



}
